// These are meant to be typed into the REPL. You can also run
// scala -Xnojline < 11_mvalue.sc to run them all at once.

case class Currency(value: Double, unit: String)

val amt = Currency(1000.0, "EUR")

// 中置表达式
amt match { case a Currency u => a + " " + u }
//res0: String = 1000.0 EUR

// 这样就不行，为什么？？？
//case class Currency2(value: Double, unit: String, talk: String, show: Double)
//val amt2 = Currency2(1000.0, "EUR", "OK", 200.0)
//// 中置表达式
//amt2 match { case  u Currency2 s  => "" }

val lst = List(1, 7, 2, 9)
lst match { 
  case h :: t => h + t.length 
  case _ => 0
}
// h=1, t=(7,2,9)
//res1: Int = 4

/**
 * h=1, t=(7,2,9)
 */
lst match { 
  case ::(h, t) => println(h); println(t)
  case _ => 0
}
// 1
//List(7, 2, 9)
//res78: AnyVal = ()


// :: 将元素添加到List最前面

// 从前往后匹配
List(1, 7, 2, 9) match { 
  case first :: second :: rest => println(first); println(second); println(rest)
  case _ => 0
}
//1
//7
//List(2, 9)
//res79: AnyVal = ()

List(1, 7, 2, 9) match { 
  case ::(first, ::(second, rest)) => println(first); println(second); println(rest)
  case _ => 0
}
//1
//7
//List(2, 9)
//res80: AnyVal = ()


List(List(1, 7), List(2, 9)) match { 
  case (first :: second) :: rest1 => println(first); println(second); println(rest1)
  case _ => 0
}
//1
//List(7)
//List(List(2, 9))
//res81: AnyVal = ()

// Infix notation works with any binary unapply--doesn't have to 
// come from case class

case object +: {
  def unapply[T](input: List[T]) = 
    if (input.isEmpty) None else Some((input.head, input.tail))
}

1 +: 7 +: 2 +: 9 +: Nil match { 
  case first +: second +: rest => println(first); println(second); println(rest)
}
//1
//7
//List(2, 9)
